<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
 * Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
 * RMH Homebase is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
*/

/**
 * @version May 1, 2008
 * @author Oliver Radwan and Allen Tucker
 */

include_once('dbinfo.php');
include_once('Person.php');

function setup_dbPersons() {
		connect();
		mysql_query("DROP TABLE IF EXISTS dbPersons");
		mysql_query("CREATE TABLE dbPersons(id TEXT NOT NULL,first_name TEXT NOT NULL, last_name TEXT, " .
				"    address TEXT, city TEXT, state VARCHAR(2), zip TEXT, phone1 VARCHAR(12) NOT NULL, phone2 VARCHAR(12), " .
				"    email TEXT, type TEXT, background_check TEXT, interview TEXT, shadow TEXT, " .
				"    convictions TEXT, wherelived TEXT, experience TEXT, motivation TEXT, specialties TEXT, " .
				"    availability TEXT, schedule TEXT, history TEXT, " .
				"    birthday TEXT, start_date TEXT, public_notes TEXT, my_notes TEXT, private_notes TEXT, password TEXT)");
	$admin_acct = new Person('admin','admin',null,null,null,null,null,null,null,'manager',
			null,null,null,null,null,null,null,null,null,null,null,null,null,
			'This is the default admin account.  It should not be used for anything but the initial editing, and then it should be deleted.',
			null,null,'admin');
	$result=add_person($admin_acct);
    if(!$result)
        echo mysql_error();
    else echo("<br />Admin account created.  username = admin, password = admin.<br />");
//    mysql_close();
}
/*
 * add a person to dbPersons table: if already there, return false
 */
	function add_person($person){
		if(! $person instanceof Person) die("Error: add_person type mismatch");
		connect();
		$query = "SELECT * FROM dbPersons WHERE id = '".$person->get_id()."'";
		$result = mysql_query($query);
		//if there's no entry for this id, add it
		if ($result==null || mysql_num_rows($result) == 0) {
   			mysql_query('INSERT INTO dbPersons VALUES("'.
		             $person->get_id().'","'.
		             $person->get_first_name().'","'.
		             $person->get_last_name().'","'.
		             $person->get_address().'","'.
		             $person->get_city().'","'.
		             $person->get_state().'","'.
		             $person->get_zip().'","'.
		             $person->get_phone1().'","'.
		             $person->get_phone2().'","'.
		             $person->get_email().'","'.
		             implode(',', $person->get_type()).'","'.
		             $person->get_background_check().'","'.
		             $person->get_interview().'","'.
		             $person->get_shadow().'","'.
		             $person->get_convictions().'","'.
		             $person->get_wherelived().'","'.
		             $person->get_experience().'","'.
		             $person->get_motivation().'","'.
		             $person->get_specialties().'","'.
		             implode(',', $person->get_availability()).'","'.
		             implode(',', $person->get_schedule()).'","'.
		             implode(',', $person->get_history()).'","'.
		             $person->get_birthday().'","'.
		             $person->get_start_date().'","'.
                     $person->get_public_notes().'","'.
                     $person->get_my_notes().'","'.
                     $person->get_private_notes().'","'.
                     $person->get_password().
                     '");');
            mysql_close();
            return true;
   		}
   		mysql_close();
   		return false;
   	}
/*
 * remove a person from dbPersons table.  If already there, return false
 */
	function remove_person ($id) {
		connect();
   		$query = 'SELECT * FROM dbPersons WHERE id = "'. $id . '"';
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) == 0) {
		   mysql_close();
		   return false;
		}
		$query='DELETE FROM dbPersons WHERE id = "'.$id.'"';
		$result=mysql_query($query);
		mysql_close();
		return true;
	}
/*
 * @return a single row from dbPersons table matching a particular id.
 * if not in table, return false
 */
	function get_person($id){
		connect();
   		$query = 'SELECT * FROM dbPersons WHERE id = "'.$id.'"';
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) !== 1) {
		   mysql_close();
		   return false;
		}
		mysql_close();
   		return $result;
	}

	function change_password($id,$newPass){
		connect();
		$query = 'UPDATE dbPersons SET password = "'.$newPass.'" WHERE id = "'.$id.'"';
		$result = mysql_query($query);
		mysql_close();
		return $result;
	}
/*
 * @return all rows from dbPersons table ordered by last name
 * if none there, return false
 */

	function getall_persons(){
		connect();
   		$query = "SELECT * FROM dbPersons ORDER BY last_name,first_name";
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) == 0) {
		   mysql_close();
		   return false;
		}
		mysql_close;
   		return $result;
	}

/*
 * @return all people of type $t from dbPersons table ordered by last name
 */
	function getall_type($t){
		connect();
   		$query = "SELECT * FROM dbPersons WHERE type LIKE '%" . $t . "%' ORDER BY last_name,first_name";
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) == 0) {
		   mysql_close();
		   return false;
		}
		mysql_close;
   		return $result;
	}
/*
 *   get all volunteers available for venue $venue on a particular $day and $time
 *   $venue == "" means House and $venue == "Fam" means Family Room.
 *   return false if none are available
 */
function getall_available($venue, $day, $time) {
	connect();
if ($venue=="Fam")
		$query="SELECT * FROM dbPersons WHERE (type LIKE '%family_room%')" .
			" AND availability LIKE '%" . $day . $time .
			"%' ORDER BY last_name,first_name";
	else
		$query="SELECT * FROM dbPersons WHERE (NOT (type LIKE '%family_room%'))" .
			" AND availability LIKE '%" . $day . $time .
			"%' ORDER BY last_name,first_name";
		$result=mysql_query($query);
	mysql_close();
	return $result;
}
/*
 *   get all available volunteers having either type $t or type $u
 *   return false if none are available
 */
function getall_eithertype ($t, $u) {
	connect();
	$query="SELECT * FROM dbPersons WHERE (type LIKE '%" .
		$t . "%' OR type LIKE '%" . $u .
		"%') ORDER BY last_name,first_name";
	$result=mysql_query($query);
	mysql_close();
	return $result;
}

?>
